package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.User;
import com.conversationboard.tree.Folders;
import com.conversationboard.tree.Node;

@WebServlet(name = "AdminEditFolderDescriptionControllerServlet", urlPatterns = "/Admin/AdminEditFolderDescriptionControllerServlet")
public class AdminEditFolderDescriptionControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 440613482383641445L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			if ((!user.isSiteAdmin()) || (!SecurityChecks.passes(request, user, this))) {
				return;
			}

			int nodeId = Integer.parseInt(request.getParameter("nodeId"));
			String folderDescription = request.getParameter("description");

			Node node = Folders.getInstance().getRoot().findFolder(nodeId);
			node.setFolderDescription(folderDescription);

			Folders.getInstance().store();

			response.sendRedirect(Configuration.getInstance().getRoot() + "/Pages/Admin/boardstructure.jsp");

		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

}